package com.ruoyi.common.utils;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * 数学算工具类
 *
 * @author ByChen
 */
public class MathUtils {
    /**
     * 计算百分比
     *
     * @param a
     * @param b
     * @return
     */
    public static String calculateThePercentage(Integer a, Integer b) {
        if (ObjectUtils.isNull(a) || ObjectUtils.isNull(b)) {
            return "0%";
        }
        if (b == 0 || a == 0) {
            return "0%";
        }
        double c = (double) a / (double) b;
        DecimalFormat df = (DecimalFormat) NumberFormat.getPercentInstance();
        //小数点两位
        df.applyPattern("0%");
        //精确到小数点后两位
        df.setMaximumFractionDigits(1);
        return df.format(c);
    }
}
